from urllib.request import urlopen, urlretrieve
import re
import os

def download_img_from_url(url):
    '''
    if url match mattern, resolve its md5,
    else calc md5 to url.
    use {md5}.jpg as filename
    return filepath to image and http
    '''
    pattern=r'https?://(.*?)/(.*?)/(.*?)/(.*?)-(.*?)-(?P<md5>.*?)/(.*?)'
    ret = re.match(pattern,url)
    if ret:
        res = ret.groupdict()
        filename='{}.jpg'.format(res['md5'])
    else:
        import hashlib
        m = hashlib.md5()
        m.update(url.encode())
        filename='{}.jpg'.format(m.hexdigest())
    abs_path = os.path.abspath(os.path.join("data", filename))
    abs_path = os.path.join("data",filename)
    print(abs_path)
    # return urlretrieve(url,filename)
    return urlretrieve(url)

if __name__ == '__main__':
    # urlretrieve('https://gchat.qpic.cn/gchatpic_new/24822261/2635401125-3089189655-3EE109923F3F0F9E7CF63EEF54A54F87/0','./test_img_download.jpg')
    url='https://gchat.qpic.cn/gchatpic_new/24822261/2635401125-3089189655-3EE109923F3F0F9E7CF63EEF54A54F87/0'
    filename,_ = download_img_from_url(url)
    print(filename)